package com.indexingsystem.system.web.security;
//package com.indexingsystem.system.web.security;
//
//import java.util.Set;
//
//import javax.annotation.Resource;
//
//import org.apache.commons.lang.StringUtils;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.AccountException;
//import org.apache.shiro.authc.AuthenticationException;
//import org.apache.shiro.authc.AuthenticationInfo;
//import org.apache.shiro.authc.AuthenticationToken;
//import org.apache.shiro.authc.DisabledAccountException;
//import org.apache.shiro.authc.SimpleAuthenticationInfo;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.springframework.stereotype.Component;
//
//import com.indexingsystem.boss.Vo.PUserPwVo;
//import com.indexingsystem.boss.entity.Pusers;
//import com.indexingsystem.boss.token.ShiroToken;
//import com.indexingsystem.boss.token.manager.TokenManager;
//import com.indexingsystem.boss.utils.DateUtil;
//import com.indexingsystem.system.service.IOperationsService;
//import com.indexingsystem.system.service.IPusersService;
//import com.indexingsystem.system.service.IRolesService;
//
///**
// * 用户身份验证,授权 Realm 组件
// * 
// * @author administrator
// * @since 2014年6月11日 上午11:35:28
// **/
////@Component(value = "securityRealm")
//public class SecurityRealm extends AuthorizingRealm {
//
//	// @Resource
//	// private IPusersService userService;
//	//
//	// @Resource
//	// private IRolesService roleService;
//	//
//	// @Resource
//	// private PermissionService permissionService;
//
//	@Resource
//	private IPusersService iPusersService;
//
//	// @Autowired
//	// PermissionService permissionService;
//
//	@Resource
//	private IOperationsService iOperationsService;
//
//	@Resource
//	private IRolesService iRolesService;
//
//	/**
//	 * 权限检查
//	 */
//	@Override
//	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//		String username = String.valueOf(principals.getPrimaryPrincipal());
//		String userId = TokenManager.getUserId();
//		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//		if (StringUtils.isNotBlank(userId)) {
//			// 根据用户ID查询角色（role），放入到Authorization里。
//			Set<String> roles = iRolesService.findRoleByUserId(userId);
//			info.setRoles(roles);
//			// 根据用户ID查询权限（permission），放入到Authorization里。
//			Set<String> permissions = iOperationsService.findPermissionByUserId(userId);
//			info.setStringPermissions(permissions);
//		} else {
//			SecurityUtils.getSubject().logout();
//		}
//		return info;
//	}
//
//	/**
//	 * 登录验证
//	 */
//	@Override
//	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
//			throws AuthenticationException {
//		String username = String.valueOf(authcToken.getPrincipal());
//		String password = new String((char[]) authcToken.getCredentials());
//		// // 通过数据库进行验证
//		// final User authentication = userService.authentication(new
//		// Pusers(username, password));
//		// if (authentication == null) {
//		// throw new AuthenticationException("用户名或密码错误.");
//		// }
//		ShiroToken token = (ShiroToken) authcToken;
//		PUserPwVo puserPwVo =new PUserPwVo();
//    	puserPwVo.setPassword(password);
//    	puserPwVo.setUserName(username);
//        Pusers user = iPusersService.login(puserPwVo);
//		if (null == user) {
//			throw new AccountException("帐号或密码不正确！");
//			/**
//			 * 如果用户的status为禁用。那么就抛出<code>DisabledAccountException</code>
//			 */
//		} else if (Pusers._0.equals(user.getStatus())) {
//			throw new DisabledAccountException("帐号已经禁止登录！");
//		} else {
//			// 更新登录时间 last login time
//			user.setLastLoginDate(DateUtil.parseFormatToDate(DateUtil.getCurTotalDate()));
//			iPusersService.updateByPrimaryKeySelective(user);
//		}
//		return new SimpleAuthenticationInfo(user, password, getName());
//	}
//}